package com.gxl.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.gxl.common.Constants;
import com.gxl.pojo.Account;
import com.gxl.pojo.TransferLog;
import com.gxl.service.AccountService;
import com.gxl.service.TransferLogService;
import com.gxl.service.impl.AccountServiceImpl;
import com.gxl.service.impl.TransferLogServiceImpl;

/**
 * ת��
 * @author set_free
 *
 */
@WebServlet("/transfer")
public class TransferServlet extends HttpServlet{
	private AccountService asi = new AccountServiceImpl();
	private TransferLogService ts= new TransferLogServiceImpl();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		//post
		req.setCharacterEncoding("utf-8");
		res.setCharacterEncoding("utf-8");
		//get方式乱码处理
		String n1 = req.getParameter("name");
		if(null != n1 && n1.length() > 0){
			String name = new String(n1.getBytes("iso-8859-1"),"utf-8");
		}
		
		double needmoney = 100d;
		Account accout = new Account();
		accout.setAccount("1");
		accout.setPassword("ps");
		accout.setBalance(needmoney);
		
		Account accin = new Account();
		accin.setAccount("2");
		accin.setName("li4");
		accin.setBalance(needmoney);
		
		String transfer = asi.transfer(accout, accin);
		if(transfer.equals(Constants.TRANSFER_SUCCESS)){
			TransferLog tl = new TransferLog();
			tl.setAccin("2");
			tl.setAccout("1");
			tl.setMoney(100d);
			ts.insTransferLog(tl);
			Logger logger = Logger.getLogger(TransferServlet.class);
			logger.info("转账金额："+needmoney);
//			res.getWriter().write("alert('转账金额')");
			res.getWriter().write("<script>alert('转账金额:"+needmoney+"')</script>");
		}else{
			System.out.println(transfer);
			res.getWriter().write("<script>alert('转账失败')</script>");
		}
	}
}
